<template>
    <div class="detail-box">
        <el-text type="primary" size="large">
            <h1 class="my-h1">商品详情</h1>
        </el-text>

        <br>
        <el-row class="detail-row">
            <el-col :span="12">
                <el-image class="detail-img" :src="goods.cover" fit="cover">
                    <template #error>
                        <div class="image-slot" style="font-size: 30px;">
                            <el-icon>
                                <Picture />
                            </el-icon>
                        </div>
                    </template>
                </el-image>
            </el-col>
            <el-col :span="12">
                <el-row>
                    <h5 class="detail-top">
                        <span v-if="type == {}">{{ "【单品】" + goods.name }}</span>
                        <span v-else>{{ "【" + type.name + "】 " + goods.name }}</span>
                    </h5>
                </el-row>
                <el-row>
                    <el-text class="detail-intro">
                        <el-tooltip class="box-item" :content="goods.intro" placement="top-start">
                            {{ goods.intro }}
                        </el-tooltip>
                    </el-text>
                </el-row>
                <el-row>
                    <el-text class="detail-price" line-clamp="3">
                        <span style="font-size: 28px;">￥</span>{{ goods.price }}
                    </el-text>
                </el-row>
                <div style="margin: 0 auto;text-align: center;height: 200px;font: 700 28px/200px 微软雅黑;color: var(--el-text-color-regular);"
                    v-if="goods.stock == 0">
                    <span>该商品库存不足，静待补货</span>
                </div>
                <div v-else>
                    <el-row>
                        <el-text class="detail-num">
                            <span style="display: inline-block;margin-right: 50px;">数量: </span>
                            <el-input-number v-model="num" :min="1" :max="10 < goods.stock ? 10 : goods.stock" />
                            <span style="color: gray;font-weight: 400;margin-left: 20px;">{{ "库存" + goods.stock + "件"
                            }}</span>
                        </el-text>
                    </el-row>
                    <el-row style="justify-content: center;">
                        <el-button type="primary" class="detail-button" :disabled="goods.stock == 0" @click="addCart">加入购物车</el-button>
                    </el-row>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';

export default {
    data() {
        return {
            goods: {
                // "gid": 1,
                // "tid": 1,
                // "name": "草莓蛋糕",
                // "cover": "img7.jpg",
                // "image1": "\"\"",
                // "image2": "\"\"",
                // "price": 89.9,
                // "intro": "商品描述",
                // "stock": 9,
                // "createTime": "2023-12-15T14:26:14",
                // "updateTime": "2023-12-15T14:26:14",
            },
            type : {},
            num: 1,
        }
    },
    mounted() {

        const search = window.location.search;
        const params = new URLSearchParams(search);

        // 遍历查询字符串中的每个参数  
        params.forEach((value, key) => {
            if (key === "goods") {
                this.goods = JSON.parse(value);
            }
            if (key === "type") {
                this.type = JSON.parse(value);
            }
        });

        let state = JSON.parse(localStorage.getItem('store'));
        state.navActive = this.$store.state.navActive;
        this.$store.replaceState(state);
    },
    methods: {
        isLogin() {
            if (!this.$store.state.isLogin) {
                ElMessageBox.alert('此操作需要登录才能进行，是否前往登录?', '提示', {
                    confirmButtonText: '前往登录',
                    callback: (action) => {
                        if (action === "confirm") {
                            this.$store.state.navActive = '/login';
                        }
                    },
                })
                return false;
            } else {
                return true;
            }
        },
        addCart() {
            if (this.isLogin()) {
                //后端
                const data = {
                    uid: this.$store.state.user.uid,
                    gid: this.goods.gid,
                    num: this.num
                }
                axios.post('/cart', data)
                    .then(result => {
                        if (result.data.code === 1) {
                            ElMessage.success("添加购物车成功");
                            this.$store.state.navActive = '/cart';
                        } else {
                            if (result.data.msg === 'goods_exist') {
                                ElMessage.info("商品在购物车中已存在");
                            }
                            console.log(result);
                        }
                    })
                    .catch(err => {
                        console.log(err);
                    })
            }
        }
    }


}
</script>

<style>
.my-h1 {
    color: #000;
    text-align: center;
    font-size: 28px;
    margin: 10px 0;
}

.detail-box {
    margin: 0 auto;
    width: 1000px;
    position: relative;
    overflow: hidden;
}

.detail-row {
    margin: 20px 0 100px 0;
    border: 1px lightgray solid;
    border-radius: 10px;
    padding: 10px;
    box-sizing: content-box;
}

.detail-top {
    font: 400 32px/100px 微软雅黑;
    width: 100%;
    min-height: 100px;
    height: 100px;
    margin: 0 auto;
}

.detail-img {
    width: 450px;
    height: 450px;
    padding: 5px;
    border: 1px lightgray solid;
    border-radius: 5px;
}

.detail-price {
    font: 700 32px/100px 微软雅黑;
    width: 100%;
    min-height: 100px;
    height: 100px;
    margin: 0 auto;
    padding-left: 50px;
    background-color: lightcyan;
}

.detail-num {
    font: 700 16px/100px 微软雅黑;
    width: 100%;
    height: 100px;
    margin: 0 auto;
    padding-left: 20px;
}

.detail-button {
    width: 70%;
    min-height: 50px;
}

.detail-intro {
    width: 100%;
    min-height: 50px;
    max-height: 100px;
    margin: 0 auto;
    margin-bottom: 10px;
    padding-left: 20px;
    padding-right: 20px;
}
</style>